//
//  ESSubscribeViewController.m
//  Eyeshot
//
//  Created by lanou3g on 15/7/8.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESSubscribeViewController.h"
#import "AFNetworking.h"
#import "ESTagsListCell.h"
#import "ESRecomendTViewController.h"
#import "ESTagDetailViewController.h"
@interface ESSubscribeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tagsLiestTableView;

@property (strong,nonatomic) NSMutableArray *tagsListArray;//列表数组
@property (strong,nonatomic) NSMutableArray *rec_tagsArray;//推荐列表数组(分区1)
@property (strong,nonatomic) NSMutableArray *def_tagsArray;//已订阅列表数组(分区2)

@end

@implementation ESSubscribeViewController

- (void)viewWillAppear:(BOOL)animated {
    self.view.backgroundColor = [UIColor colorWithRed: 234/255.0 green:234/255.0 blue: 234/255.0 alpha:1.0];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.数据
    [self requestData];
    //2.创建表
    [self createTagsListTableView];
    
    //设置观察者,观察ESTagsCell的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAlertView:) name:@"alertView" object:@"ESSubscribeViewController"];
    //设置观察者,订阅请求发送成,重新请求数据,刷新表格
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"刷新表格" object:@"ESSubscribeViewController"];
}
#pragma mark - 设置观察者,观察ESTagsCell的通知,显示提示视图

- (void)showAlertView:(NSNotification *)notification{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"温馨提示:" message:@"默认订阅标签,不能取消订阅." preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertView animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });
}

#pragma mark -请求数据
- (void)requestData{
    //1.获得请求管理者
    AFHTTPRequestOperationManager *netManager = [[AFHTTPRequestOperationManager alloc] init];
    //2.设置参数
    
    //3.发送GET请求
    [netManager GET:@"http://api.budejie.com/api/api_open.php?a=tags_list&appname=baisishequ&asid=1880212C-B451-4FA7-8DFA-E07254EFCEE9&c=subscribe&client=iphone&device=iPhone%204S&jbk=0&mac=&market=&openudid=5a299ed728ffa000f0c4db0eb49e93d0e3968b2a&udid=&ver=3.6" parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *dic) {
        //1.推荐列表数据
        _rec_tagsArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dic1 in dic[@"rec_tags"]) {
            ESTagsListModel *model = [[ESTagsListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [_rec_tagsArray addObject:model];
        }
        //2.已订阅列表数据
        _def_tagsArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dic1 in dic[@"def_tags"]) {
            ESTagsListModel *model = [[ESTagsListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [_def_tagsArray addObject:model];
        }
        //3.把两个分区数组放入大数组
        _tagsListArray = [[NSMutableArray alloc] init];
        [_tagsListArray addObject:_rec_tagsArray];
        [_tagsListArray addObject:_def_tagsArray];
        //刷新表格
        [_tagsLiestTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据请求失败:%@",error);
    }];
}

#pragma mark - 创建表视图

- (void)createTagsListTableView{
    //1.设置表代理
    _tagsLiestTableView.dataSource = self;
    _tagsLiestTableView.delegate = self;
    //2.注册cell类
    [_tagsLiestTableView registerNib:[UINib nibWithNibName:@"ESTagsListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"tagsListCell"];
}
#pragma mark - 实现表视图的协议方法
//1.2个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _tagsListArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW - 16, 30)];
    headerView.backgroundColor = [UIColor colorWithRed: 234/255.0 green:234/255.0 blue: 234/255.0 alpha:1.0];
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW - 16 - 30, 25)];
    textLabel.font = [UIFont systemFontOfSize:15];
    [headerView addSubview:textLabel];
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeSystem];
    bt.frame = CGRectMake(kScreenW - 44, 0, 20, 20);
    [bt addTarget:self action:@selector(headerBT:) forControlEvents:UIControlEventTouchUpInside];
    [bt setImage:[UIImage imageNamed:@"accessory"] forState:UIControlStateNormal];
    
    switch (section) {
        case 0:
            textLabel.text = @"推荐标签";
            [headerView addSubview:bt];
            break;
        case 1:
            textLabel.text = @"已订阅标签";
            break;
    }
    return headerView;
}
- (void)headerBT:(UIButton *)button{
    NSLog(@"进入推荐标签");
    //进入推荐标签列表
    ESRecomendTViewController *recommentVC = [[ESRecomendTViewController alloc] init];
    [self.navigationController pushViewController:recommentVC animated:YES];
}
//2.每个分区
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_tagsListArray[section] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseIdentifer = @"tagsListCell";
    ESTagsListCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
    if (cell == nil) {
        cell = [[ESTagsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];
    }
    cell.tagsListModel = _tagsListArray[indexPath.section][indexPath.row];
    cell.theme_id = [_tagsListArray[indexPath.section][indexPath.row] theme_id];//把theme_id传到ESTagsListCell
    cell.is_default = [_tagsListArray[indexPath.section][indexPath.row] is_default];//把theme_id传到ESTagsListCell
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //点击cell,进入标签详情
    ESTagDetailViewController *tagDetailVC =[[ESTagDetailViewController alloc] init];
    tagDetailVC.theme_id = [_tagsListArray[indexPath.section][indexPath.row] theme_id];//主题ID传到下一层
    [self.navigationController pushViewController:tagDetailVC animated:YES];
}

@end
